<?php

/**
 * PasswordResetForm class.
 * PasswordResetForm is the data structure for resetting user password.
 */
class PasswordResetForm extends CFormModel {

    public $username;
    public $verifyCode;
    
    /**
     * Declares the validation rules.
     */
    public function rules() {
        return array(
            // username is required
            array('username', 'required'),
            // determine whether username exists
            array('username', 'usernameExist'),
            // captcha
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements()),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'username' => 'Username',
            'verifyCode' => 'Verification Code',
        );
    }

    /**
     * Determine whether username exists.
     */
    public function usernameExist($attribute, $params) {
        $user = User::model()->find('username = :username', array(':username' => $this->username));

        if ($user === null)
            $this->addError('username', 'Username does not exist!');
    }
    
}
